package com.system.boot.setting.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.system.api.SuperInterface;
import com.system.api.setting.entity.po.SysIcon;
import com.system.boot.rbac.service.UserService;
import com.system.boot.setting.mapper.IconMapper;
import com.system.boot.setting.service.IconService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.yang.common.core.exception.BaseException;
import org.yang.common.i18n.constant.I18nMessageConstant;
import org.yang.database.orm.entity.BaseModel;
import org.yang.database.orm.entity.BaseQuery;

import java.util.*;

import static com.system.common.constant.MessageConstant.ICON_CODE_EMPTY;

/**
 * 图标实现
 *
 * @author: lslands
 * @version: 1.0
 * @create: 2024/5/16
 * @description:
 */
@Service
@RequiredArgsConstructor
public class IconServiceImpl extends ServiceImpl<IconMapper, SysIcon> implements IconService, SuperInterface<SysIcon> {



    @Override
    public void isExists(String code) throws BaseException {
        boolean existsByCode = baseMapper.getExistsByCode(code);
        if(existsByCode){
            throw new BaseException(BaseException.REPEAT,code);
        }
    }

    @Override
    public int handInsert(SysIcon record) throws BaseException {
        String code =Optional.ofNullable(record.getCode())
                        .orElseThrow(() -> new BaseException(ICON_CODE_EMPTY));
        this.isExists(code);
        return baseMapper.insert(record);
    }

    @Override
    public int handUpdateById(SysIcon record) throws BaseException {
        SysIcon oldResult = Optional.ofNullable(baseMapper.selectById(record.getId()))
                        .orElseThrow(() -> new BaseException(BaseException.NULL));
        if (!Objects.equals(record, oldResult)) {
            String code = Optional.ofNullable(record.getCode())
                    .orElseThrow(() -> new BaseException(ICON_CODE_EMPTY));
            if (!code.equals(oldResult.getCode())) {
                this.isExists(record.getCode());
            }
            return baseMapper.updateById(record);
        }
        return 1;
    }

    @Override
    public int handEnabledById(Long id, Integer enabled, Integer revision) throws BaseException {
        SysIcon oldResult = Optional.ofNullable(baseMapper.selectById(id))
                .orElseThrow(()-> new BaseException(BaseException.NULL));
        if(null==enabled){
            throw new BaseException(I18nMessageConstant.VALIDATION_STATUS_EMPTY);
        }
        if(enabled.equals(oldResult.getEnabled())){
            return 1;
        }
        return baseMapper.updateById(new SysIcon(id, enabled, revision));
    }

    @Override
    public Page<SysIcon> getPage(BaseQuery query, SysIcon record) throws BaseException {
        Page<SysIcon> page = new Page<>(query.getCurrent(), query.getSize());
        String type = record.getType();
        LambdaQueryWrapper<SysIcon> wrapper = new LambdaQueryWrapper<SysIcon>()
                .like(StringUtils.isNotBlank(record.getName()), SysIcon::getName, record.getName())
                .in(StringUtils.isNotBlank(type),SysIcon::getType,Collections.singletonList(type))
                .orderByDesc(BaseModel::getId);
        return baseMapper.selectPage(page, wrapper);
    }


    @Override
    public int deleteOne(Long id) throws BaseException {
        if( Objects.isNull(id)){
            throw new BaseException("id cannot be null");
        }
        return baseMapper.deleteById(id);
    }

    @Override
    public int deleteBatch(Boolean delete,List<Long> ids) throws BaseException {
        if(ids.isEmpty()){
            return 0;
        }
        if(delete){
            return baseMapper.deleteBatch(ids);
        }
        return baseMapper.deleteBatchIds(ids);
    }

}
